{include file="public/head"}

<body>
    <div class="centInput">
        <input type="text" placeholder="请输入姓名" name="true_name" />
        <input type="text" placeholder="请输入身份证号" name="id_card" />
        <input type="text" placeholder="请输入电话号码" name="phone" />
        <div class="centInput-code">
            <input type="text" class="centInput-code-left floatLeft" placeholder="请输入验证码" name="code" />
            <input type="button" id="btn" value="获取验证码" class="floatRight" />
        </div>
        <input type="hidden" name="ass_id" value="{$ass_id}">
        <input type="hidden" name="type" value="{$type}">
    </div>
    <div class="submitCent">
        提交并支付
    </div>
    <script src="__STATIC__/home/js/jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>
    <script src="__STATIC__/home/js/swiper.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
        $(function () {
            $('.submitCent').show();
            $('#btn').click(function () {
                var phone = $('input[name="phone"]').val();
                if (!(/^\d{11}$/.test(phone))) {
                    alert('请输入正确电话号码');
                    return false;
                }
                // 短信
                $.ajax({
                    type: 'post',
                    url: '/index/Makeupapi/phoneSend',
                    data: {
                        phone
                    },
                    success(res) {
                        var Time = 60;
                        var aa = setInterval(bb, 1000);
                        function bb() {
                            $("#btn").attr("disabled", true);
                            $("#btn").val(Time + "秒后重发");
                            if (Time == 0) {
                                $("#btn").val("发送验证码").removeAttr("disabled");
                                clearInterval(aa);
                            }
                            Time--;
                        }
                        if (res.code == 0) {
                            alert('发送失败，请重试');
                            clearInterval(aa);
                        }
                    },
                    error() {
                        alert('短信接口错误');
                    }
                });
            });
            // 提交支付
            $('.submitCent').click(function () {
                var true_name = $('input[name="true_name"]').val();
                if (!true_name) {
                    alert('请输入姓名');
                    return false;
                }
                var id_card = $('input[name="id_card"]').val();
                if (!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/).test(id_card)) {
                    alert('请输入正确身份证');
                    return false;
                }
                var phone = $('input[name="phone"]').val();
                if (!(/^\d{11}$/.test(phone))) {
                    alert('请输入正确电话号码');
                    return false;
                }
                var code = $('input[name="code"]').val();
                if (!code) {
                    alert('请输入验证码');
                    return false;
                }
                new Promise((resolve) => token(resolve)).then(e => {
                    $.ajax({
                        type: 'post',
                        url: '/index/assemble/orderDown',
                        data: {
                            true_name,
                            id_card,
                            phone,
                            ass_id: $('input[name="ass_id"]').val(),
                            type: $('input[name="type"]').val(),
                            code,
                            __token__: e,
                        },
                        success(res) {
                            if (res.code == 1) {
                                window.location.href = res.jump_url;
                            } else if (res.code == 1000) {
                                alert('您已提交过，请刷新页面重试！');
                            } else {
                                alert(res.msg);
                            }
                        }, error() { alert('提交支付接口错误！'); }
                    })
                });
            });

            // token令牌
            function token(cb) {
                $.ajax({
                    type: 'post',
                    url: '/index/Makeupapi/token',
                    success(res) {
                        if (res.code == 1) {
                            cb(res.__token__);
                        }
                    },
                    error() { alert('token令牌接口错误'); }
                })
            };

        });
    </script>
</body>

</html>